angular.module('app').component('winitFlowPanel', {
    templateUrl: getTemplateUrl('matrix/component/tpl/winitFlowPanel.html'),
    bindings: {

    },
    controller: function ($scope, $rootScope, $stateParams, $http, $modal, lodash) {
        var ctrl = this;
        $scope.info = {
            data : [
                {"id": 1, "user": "卖家", "path": "TOM>OMS>出库单管理>A1", "pathId": 1, "desc": "出库单A1的描述！！！"},
                {"id": 2, "user": "卖家", "path": "TOM>OMS>出库单管理>A2", "pathId": 2, "desc": "出库单A2的描述！！！"},
                {"id": 3, "user": "卖家", "path": "TOM>OMS>出库单管理>A3", "pathId": 3, "desc": "出库单A3的描述！！！"},
                {"id": 4, "user": "卖家", "path": "TOM>OMS>出库单管理>A4", "pathId": 4, "desc": "出库单A4的描述！！！"},
                {"id": 5, "user": "卖家", "path": "TOM>OMS>出库单管理>A5", "pathId": 5, "desc": "出库单A5的描述！！！"},
                {"id": 6, "user": "卖家", "path": "TOM>OMS>出库单管理>A6", "pathId": 6, "desc": "出库单A6的描述！！！"}],
            pathList: [
                        {"id": 1, "name": "A1", "path": "TOM>OMS>出库单管理>A1"},
                        {"id": 2, "name": "A2", "path": "TOM>OMS>出库单管理>A2"},
                        {"id": 3, "name": "A3", "path": "TOM>OMS>出库单管理>A3"},
                        {"id": 4, "name": "A4", "path": "TOM>OMS>出库单管理>A4"},
                        {"id": 5, "name": "A5", "path": "TOM>OMS>出库单管理>A5"},
                        {"id": 6, "name": "A6", "path": "TOM>OMS>出库单管理>A6"}],
            isView: false,
            isMaster: false,
            selectedPath: {},
            isDisabled: true,
        };

        $scope.switchViewChange = function(status) {
            $scope.info.isView  = status;
        };

        $scope.switchMasterChange = function(status) {
            $scope.info.isMaster = status;
        };

        $scope.clickNode = function(data) {
            console.log(data);
            $scope.info.isDisabled = false;
            $(".select-node[data-id!="+ data.id +"]").css("display", "none");
            $(".select-node[data-id="+ data.id +"]").css("display", "block");
            //$scope.openModal(data.id);
        };

        $(document).click(function(e){
            var elementStr = $(e.target).context.className;
            var hasEle = elementStr.indexOf("btn-click");
            if(hasEle == "-1") {
                $scope.info.isDisabled = true;
                $(".select-node").css("display", "none");
            }
        });

        $scope.clickShowNode = function(data) {
            $(".show-node[data-id!="+ data.id +"]").css("display", "none");
            $(".show-node[data-id="+ data.id +"]").css("display", "block");
        };

        $scope.removeFlowItem = function(id) {
            $scope.info.data.splice($.inArray(id, $scope.info.data), 1);
        };

        //---------test table
        $scope.isNewTr = false;
        $scope.addTr = function() {
            console.log("addTr");
            var newTr = {"id": "", "user": "", "path": "", "pathId": "", "desc": "", "isNewTr": true};
            var index = "";
            $scope.info.data.push(newTr);
        };

        $scope.clickEditBtn = function(data, index) {
            console.log("clickEditBtn");
            $("tr[data-id="+ index +"]").find("button[data-status='true']").addClass("hide");
            $("tr[data-id="+ index +"]").find("button[data-status='false']").removeClass("hide");
            $("tr[data-id="+ index +"]").find("span[data-status='true']").addClass("hide");
            $("tr[data-id="+ index +"]").find("input[data-status='false']").removeClass("hide");
            $("tr[data-id="+ index +"]").find("div[data-status='false']").removeClass("hide");
            $("tr[data-id="+ index +"]").find("textarea[data-status='false']").removeClass("hide");
            angular.forEach($scope.info.pathList, function(value) {
                if(value.id == data.pathId) {
                    $scope.info.selectedPath = value;
                }
            });
        };

        $scope.clickDeleteBtn = function(data, index) {
            console.log("clickDeleteBtn");
            $scope.info.data.splice($.inArray(data.id, $scope.info.data), 1);
        };

        $scope.clickSaveBtn = function(data, index) {
            console.log("clickSaveBtn");
            angular.forEach($scope.info.data, function(value) {
                if(value.id == data.id) {
                    value.user   = data.user;
                    value.desc   = data.desc;
                    value.path   = $scope.info.selectedPath.path;
                    value.pathId =  $scope.info.selectedPath.id;
                }
            });
            if(data.isNewTr) {
                delete data.isNewTr;
            }
            $scope.clickCancelBtn(data, index);
        };

        $scope.clickCancelBtn = function(data, index) {
            console.log("clickCancelBtn");
            $("tr[data-id="+ index +"]").find("button[data-status='true']").removeClass("hide");
            $("tr[data-id="+ index +"]").find("button[data-status='false']").addClass("hide");
            $("tr[data-id="+ index +"]").find("span[data-status='true']").removeClass("hide");
            $("tr[data-id="+ index +"]").find("input[data-status='false']").addClass("hide");
            $("tr[data-id="+ index +"]").find("div[data-status='false']").addClass("hide");
            $("tr[data-id="+ index +"]").find("textarea[data-status='false']").addClass("hide");
        };
        //---------end table

        //----flow modal
        $scope.addFlow = function() {
            flowModal("", "", "'");
        };

        function flowModal(selectedBranch, sys, reqId) {
            var modalInstance = $modal.open({
                animation: true,
                templateUrl: getTemplateUrl("matrix/component/tpl/addFlow.html"),
                controller: "addFlowCtrl",
                keyboard: true,
                backdrop: true,
                resolve: {
                    branch: function () {
                        return selectedBranch;
                    },
                    sysName: function() {
                        return sys;
                    },
                    reqId: function() {
                        return reqId;
                    }
                }
            });

            //点击modal新增后，把feature值传过来
            modalInstance.result.then(function(flow) {
                createFlow(flow);
            }, function(reason) {
                cancelModal(reason);
            });
        };

        function createFlow(flow) {
            var newItem = {
                "id": $scope.info.data.length+1,
                "user": "新添加",
                "path": flow.path,
                "pathId": flow.pathId,
                "desc": flow.desc,
            };
            $scope.info.data.push(newItem);
            $rootScope.toasterSuccessInfo("创建成功！");
        };

        function cancelModal(reason) {
            console.log("取消");
        };
        //----end flow modal
    }
});


app.controller("addFlowCtrl", ["$scope", "$rootScope", "$modalInstance", "$http", "branch", "sysName", "reqId", function($scope, $rootScope, $modalInstance, $http, branch, sysName, reqId) {
    $scope.featureList = [];
    $scope.flow = {
        name   : "",
        path   : "",
        pathId : "",
        desc   : "",
        feature: {}
    };

    getFeatureList();

    function getFeatureList() {
        $rootScope.showPanelLoading();
        var url = matrix_product_url +"/feature/getFeature?withAttr=0";
        $http.get(url).success(function(result) {
            if(result.code == "0") {
                if(result.data) {
                    $scope.featureList = result.data;
                } else {
                    $rootScope.toasterErrorInfo("获取feature数据失败！");
                }
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }
            $rootScope.hidePanelLoading();
        });
    };

    $scope.createClick = function() {
        var currentFeature = JSON.parse($scope.flow.feature);
        $scope.flow.path   = currentFeature.path;
        $scope.flow.pathId = currentFeature.id;
        delete $scope.flow.feature;
        $modalInstance.close($scope.flow);
    };

    $scope.cancelClick = function() {
        $modalInstance.dismiss("cancel");
    };
}]);